package initialize

import (
	"fmt"
	"github.com/olivere/elastic/v7"
	"go.uber.org/zap"
	"hosptial-record/global"
	"log"
	"os"
)

func InitES() {
	fmt.Println("-------------------------------")
	conf := global.ServerConfig.EsConfig
	// 初始化连接
	host := fmt.Sprintf("http://%s:%d", conf.Host, conf.Port)
	// 日志
	logger := log.New(os.Stdout, "shop", log.LstdFlags)
	var err error
	global.EsClient, err = elastic.NewClient(
		elastic.SetURL(host),
		elastic.SetSniff(false),
		elastic.SetTraceLog(logger),
	)
	if err != nil {
		zap.S().Error("ES连接失败")
		panic(err)
	}
	// 新建mapping和index
	//createEsGoodsMapping()

}

//func createEsGoodsMapping() {
//	// 判断是否存在index
//	exists, err := global.EsClient.IndexExists(model.EsGoods{}.GetIndexName()).Do(context.Background())
//	if err != nil {
//		panic(err)
//	}
//	if !exists {
//		// 创建mapping
//		esGoods := model.EsGoods{}
//		_, err = global.EsClient.CreateIndex(esGoods.GetIndexName()).BodyString(esGoods.GetMapping()).Do(context.Background())
//		if err != nil {
//			fmt.Println(err)
//			panic("EsGoods创建Mapping失败")
//		}
//	}
//}
